#include <iostream>
#include <map>

#include "BrowseItemMenu.h"
#include "ItemActionMenu.h"

using std::map;
using std::cout;
using std::endl;

//Constructors

BrowseItemMenu::BrowseItemMenu(Stock* companyStock, int itemType, string itemName): SelectionMenu("Browse " + itemName + " sorted by:", "Back")
{
	map<int, string> itemHeader;
	map<int, string>::iterator itemHeaderIterator;

	companyStock -> setBufferItem(itemType);
	itemHeader = companyStock -> getItemsHeader();

	for (itemHeaderIterator = itemHeader.begin(); itemHeaderIterator != itemHeader.end(); ++itemHeaderIterator)
	{
		addSelection(itemHeaderIterator -> second);
	}

	itemTypeID = itemType;
	itemDisplayer = new ItemsBrowser();
}

//Member Functions

void BrowseItemMenu::performAction(Stock* companyStock, Cart* theCart)
{
	int userSelection;

	do {
		displayInfoBar();
		displayContent();
		cout << endl;

		userSelection = getSelectionFromUser();
		cout << endl;

		switch(userSelection)
		{
		case 0:
			break;
		default:
			if (itemTypeID == 5)
				itemDisplayer -> displayAllItems(*companyStock, userSelection - 1);
			else
				itemDisplayer -> displayATypeOfItems(*companyStock, itemTypeID, userSelection - 1);
			cout << endl;

			ItemActionMenu* actionHandler = new ItemActionMenu(itemTypeID);
			actionHandler -> performAction(companyStock, theCart);
			delete actionHandler;

			break;
		}
	} while (userSelection != 0);

	cout << endl;
}

BrowseItemMenu::~BrowseItemMenu()
{
	delete itemDisplayer;
}